﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CDefineGlobal;
using CObject;
using Plugin_Map;

namespace BUS_LAYER
{
    public class BUS_Tank
    {
        public static void CheckCollisions(AbstractTank tank, CMap map)
        {
            int xCor = tank.XTopLeftCordinate();
            int yCor = tank.YTopLeftCordinate();
            int xNewCor = xCor;
            int yNewCor = yCor;

            // wants to move right, check if possible yet
            if (tank.I_ToMove == CDefine.DIRECTION_RIGHT)
            {
                xNewCor += CDefine.TILE_SIZE;   //code cứng
                xNewCor += tank.I_Speed;
                //if (xNewCor > this.Size.Width)
                //    xNewCor = CDefine.XTOP_LEFT;

                if (BUS_Utility.CanMove(xNewCor, yNewCor, map) && BUS_Utility.CanMove(xNewCor, yNewCor + CDefine.TILE_SIZE, map))
                {
                    tank.I_Direction = CDefine.DIRECTION_RIGHT;
                    tank.I_XMove = tank.I_Speed;
                    tank.I_YMove = 0;
                }
                else
                {
                    tank.I_XMove = 0;
                    tank.I_YMove = 0;
                    tank.I_ToMove = CDefine.DIRECTION_NONE;
                }
            } // end if right

            // wants to move left, check if possible yet
            else if (tank.I_ToMove == CDefine.DIRECTION_LEFT)
            {
                xNewCor -= tank.I_Speed;
                //if (xNewCor < CDefine.XTOP_LEFT)
                //    xNewCor = CDefine.XTOP_LEFT;

                if (BUS_Utility.CanMove(xNewCor, yNewCor, map) && BUS_Utility.CanMove(xNewCor, yNewCor + CDefine.TILE_SIZE, map))
                {
                    tank.I_Direction = CDefine.DIRECTION_LEFT;
                    tank.I_XMove = -tank.I_Speed;
                    tank.I_YMove = 0;
                    //tank.I_ToMove = CDefine.DIRECTION_NONE;
                }
                else
                {
                    tank.I_XMove = 0;
                    tank.I_YMove = 0;
                    tank.I_ToMove = CDefine.DIRECTION_NONE;
                }
            } // end if left

            // wants to move down, check if possible yet
            if (tank.I_ToMove == CDefine.DIRECTION_DOWN)
            {
                yNewCor += CDefine.TILE_SIZE;   //code cứng
                yNewCor += tank.I_Speed;
                //if (yNewCor > this.Size.Height)
                //    yNewCor = CDefine.YTOP_LEFT;

                if (BUS_Utility.CanMove(xNewCor, yNewCor, map) && BUS_Utility.CanMove(xNewCor + CDefine.TILE_SIZE, yNewCor, map))
                {
                    tank.I_Direction = CDefine.DIRECTION_DOWN;
                    tank.I_XMove = 0;
                    tank.I_YMove = tank.I_Speed;
                    //tank.I_ToMove = CDefine.DIRECTION_NONE;
                }
                else
                {
                    tank.I_XMove = 0;
                    tank.I_YMove = 0;
                    tank.I_ToMove = CDefine.DIRECTION_NONE;
                }
            } // end if down

            // wants to move up, check if possible yet
            if (tank.I_ToMove == CDefine.DIRECTION_UP)
            {
                yNewCor -= tank.I_Speed;
                //if (yNewCor < CDefine.YTOP_LEFT)
                //    yNewCor = CDefine.YTOP_LEFT;

                if (BUS_Utility.CanMove(xNewCor, yNewCor, map) && BUS_Utility.CanMove(xNewCor + CDefine.TILE_SIZE, yNewCor, map))
                {
                    tank.I_Direction = CDefine.DIRECTION_UP;
                    tank.I_XMove = 0;
                    tank.I_YMove = -tank.I_Speed;
                    //tank.I_ToMove = CDefine.DIRECTION_NONE;
                }
                else
                {
                    tank.I_XMove = 0;
                    tank.I_YMove = 0;
                    tank.I_ToMove = CDefine.DIRECTION_NONE;
                }
            } // end if up

            xCor += tank.I_XMove;
            yCor += tank.I_YMove;

            if (xCor > CDefine.GAME_WIDTH) xCor = CDefine.XTOP_LEFT;
            else if (xCor < CDefine.XTOP_LEFT) xCor = CDefine.GAME_WIDTH;

            if (yCor > CDefine.GAME_HEIGHT) yCor = CDefine.YTOP_LEFT;
            else if (yCor < CDefine.YTOP_LEFT) yCor = CDefine.GAME_HEIGHT;

            if (BUS_Utility.CanMove(xCor, yCor, map))
                tank.Move(xCor, yCor);
            else
            {
                tank.I_XMove = 0;
                tank.I_YMove = 0;
                tank.I_ToMove = CDefine.DIRECTION_NONE;
            }
        }

        public static void MoveRandomComTank(CMap map)
        {
            int nCount = (CDefine.TANK_APPEAR_COUNT <= map.IMap.ArrComTank.Count) ? CDefine.TANK_APPEAR_COUNT : map.IMap.ArrComTank.Count;

            Random r = new Random();

            for (int i = 0; i < nCount; i++)
            {
                int iDir = r.Next(1, 6);    //random : 1, 2, 3, 4, 5

                if (map.IMap.ArrComTank[i].I_ToMove == CDefine.DIRECTION_NONE)
                    map.IMap.ArrComTank[i].MoveToDirection(iDir);

                CheckCollisions(map.IMap.ArrComTank[i], map);

                //bắn đạn random
                int iFire = r.Next(1, 20);    //random : 1, 2, 3 .. 99

                if (iFire == 1 && map.IMap.ArrComTank[i].Rocket.Enabled == false)
                    map.IMap.ArrComTank[i].Fire();
            }
        }
    }
}
